/**
 * 提交submit函数
 * @param elementId Form元素id
 * @param url 查询地址
 */
function submitFun(elementId, url) {
    $("#" + elementId).attr("action", url);
    $('#' + elementId).submit();
}

/**
 * 确认后提交submit函数
 * @param elementId Form元素id
 * @param url 查询地址
 * @param title 确认提示语
 */
function submitConfirmFun(elementId, url, title) {
    if (!confirm(title)) {
        return;
    }
    submitFun(elementId, url);
}

/**
 * 确认后跳转href函数
 * @param url 查询地址
 * @param title 确认提示语
 */
function hrefConfirmFun(url, title) {
    if (!confirm(title)) {
        return;
    }
    window.location.href = url;
}

/**
 * 分页翻页
 * @param pageNo
 */
function submitNewPage(pageNo){
    var form=document.pageform;
    if(jQuery("#currentPage")!=null) {jQuery("#currentPage").val(pageNo);}
    if(form!=null) {form.submit();}
}
/**
 * 激活导航菜单
 * @param elementId 当前也菜单ID
 */
function activeNavBar(elementId) {
    $("#sidebarNav >li").removeClass("active");
    $("#" + elementId).addClass("active");
}
/**
 * 排序表格
 * @param
 */
function sortingTable() {
    var sorting = "TABLE.table .sorting,TABLE.table .sorting_ASC,TABLE.table .sorting_DESC";
    $(sorting).unbind("click").bind("click", function () {
        var field = $(this).attr("data-orderField")
        var type = $(this).attr("data-orderFieldType");
        var form = $("#pageform");
        if ($("#orderField").val() == null) {
            form.append("<input type='hidden' id='orderField' name='orderField'/>");
        }
        if ($("#orderFieldType").val() == null) {
            form.append("<input type='hidden' id='orderFieldType' name='orderFieldType'/>");
        }
        $("#orderField").val(field);
        $("#orderFieldType").val(type);
        form.submit();
    });
}
/**
 * jquery.validate的简单封装
 * @param elementId 校验规则对象
 */
function jqueryValidator(rules) {
    // 必填项添加红色星号
    $.each(rules, function (index, item) {
        var obj = rules[index];
        if (obj && obj.required) {
            $("#" + index).parent().siblings("label").prepend('<span style="color:red">*</span>');
        }
    });
    $('.form-horizontal').validate({
        errorElement: 'span',
        errorClass: 'help-block',
        focusInvalid: false,
        rules: rules,

        highlight: function (element) {
            $(element).closest('.form-group').addClass('has-error');
        },

        success: function (label) {
            label.closest('.form-group').removeClass('has-error');
            label.remove();
        },

        errorPlacement: function (error, element) {
            element.parent('div').append(error);
        },

        submitHandler: function (form) {
            form.submit();
        }
    });

    $('.form-horizontal input').keypress(function (e) {
        if (e.which == 13) {
            if ($('.form-horizontal').validate().form()) {
                $('.form-horizontal').submit();
            }
            return false;
        }
    });
};

function timeFormatter(cellvalue, options, rowObject) {
    var time = new Date(parseFloat(cellvalue));
    return time.format("yyyy-MM-dd hh:mm:ss");
}


function dateTimeFormatter(cellvalue, options, rowObject) {
    var time = new Date(parseFloat(cellvalue));
    return time.format("MM-dd hh:mm:ss");
}

function subFormatter(cellvalue, options, rowObject) {
    return cellvalue.substr(0, 60) + "...";
}

function dateFormatter(cellvalue, options, rowObject) {
    var time = new Date(parseFloat(cellvalue));
    return time.format("yyyy-MM-dd");
}

/**
 * 按条件查询
 * @param cnd 条件
 * @param table   表格
 */
function findByCnd(cnd, table) {
    var postData = $("#" + table).jqGrid("getGridParam", "postData");
    $.extend(postData, cnd);

    $("#" + table).jqGrid("setGridParam", {search: true}).trigger("reloadGrid", [
        {page: 1}
    ]);
}

/**
 * 关键字查询
 * @param btn 查询按钮ID
 */
function initSearchByKeyWord(btn, table) {
    $("#" + btn).bind("click", function () {
        var keyWord = $("#keyWord").val();
        if (keyWord) {
            keyWord = keyWord.trim();
        }
        var cnd = {
            "keyWord": keyWord,
            "pause": false
        };

        //条件查询 jmq.js
        findByCnd(cnd, table);
    });
}
/**
 * 关键字查询
 * @param btn 查询按钮ID
 */
function initAppPausedByKeyWord(btn, table) {
    $("#" + btn).bind("click", function () {
        var cnd = {
            "pause": true
        };

        //条件查询 jmq.js
        findByCnd(cnd, table);
    });
}

function confirms(title, okCallback) {
    dialog({
        title: '提示',
        content: title,
        okValue: '确定',
        ok: okCallback,
        cancelValue: '取消',
        cancel: function () {
        },
        zIndex: 1041  //大于bootstrap的1040
    }).width(320).showModal();
}

function errorAlert(error) {
    dialog({
        title: '错误提示',
        content: error,
        okValue: '确定',
        ok: function () {
        },
        zIndex: 1041     //大于bootstrap的1040
    }).width(320).show();
}

function msgAlert(info) {
    dialog({
        title: '提示',
        content: info,
        okValue: '确定',
        ok: function () {
        },
        zIndex: 1041     //大于bootstrap的1040
    }).width(320).show();
}


/**
 * 表单校验
 * @param form
 * @returns {*|jQuery}
 */
function isValid(form) {
    var selector = "#" + form;
    $(selector).validator({
        ignore: 'hidden'
    });
    $(selector).trigger("validate");
    return $(selector).isValid()
}

/**
 * 异常输出
 * @param data 后台返回的数据
 */
function onException(data) {
    var info = "操作失败，请稍候再试!";
    if (data.error) {
        info = data.error;
    } else if (data.message) {
        info = data.message;
    }
    errorAlert(info);
}

/**
 * 搜索框响应回车事件
 * @param event 事件
 * @param buttonId button的ID
 */
function onEnterClick(event, buttonId) {
    if (event.keyCode == 13) {
        buttonId.click()
    }
}

/**
 * 列表分页大小回车事件
 */
function onEnterPageSizeClick() {
    $("#pageSize").keydown(function (e) {
        if (e.keyCode == 13) {
            $("#pageform").submit(); //处理事件
        }
    });
}


function getEcharts(xAxisData,yAxisData,titleText,tooltipFormatter,xAxisFormatter,yAxisFormatter) {
    var myChart = echarts.init(document.getElementById('echartsMain'));
    var option = {
        title: {
            text: titleText,
            x: 'center'
        },
        tooltip: {
            trigger: 'axis',
            formatter: tooltipFormatter
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        yAxis: {
            type: 'value',
            axisLabel: {
                formatter: yAxisFormatter
            }
        },
        xAxis: {
            type: 'category',
            axisLine: {onZero: false},
            axisLabel: {
                formatter: xAxisFormatter
            },
            boundaryGap: false,
            data: xAxisData
        },
        series: [
            {
                type: 'line',
                smooth: true,
                lineStyle: {
                    normal: {
                        width: 3,
                        shadowColor: 'rgba(0,0,0,0.4)',
                        shadowBlur: 10,
                        shadowOffsetY: 10
                    }
                },
                data:yAxisData
            }
        ]
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.hideLoading();
    myChart.setOption(option);
}